﻿using BaseDal;
using Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace BaseBll
{
    public class AssetKeepWarnBll
    {
        static readonly Lazy<AssetKeepWarnBll> bll = new Lazy<AssetKeepWarnBll>(() => new AssetKeepWarnBll());
        public static AssetKeepWarnBll Instance { get; } = bll.Value;
        private readonly AssetKeepWarnDal DalInstance;
        private AssetKeepWarnBll()
        {
            DalInstance = AssetKeepWarnDal.Instance;
        }

        /// <summary>
        /// 查询数据是否存在
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public bool IsExist(string strWhere)
        {
            return DalInstance.IsExist(strWhere);
        }

        /// <summary>
        /// 查询分页数据
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataTable GetPageList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return DalInstance.GetPageList(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<AssetKeepWarn> GetList(string strWhere)
        {
            return DalInstance.GetList(strWhere);
        }

        /// <summary>
        /// 根据id查询实体类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AssetKeepWarn GetModelById(decimal id)
        {
            return DalInstance.GetModelById(id);
        }

        /// <summary>
        /// 派单或转单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DispatchOrRelay(AssetKeepWarn model)
        {
            return DalInstance.DispatchOrRelay(model);
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <param name="userName">审批人</param>
        /// <returns></returns>
        public int UpdateStatus(decimal id, int status, string userName)
        {
            return DalInstance.UpdateStatus(id, status, userName);
        }

        /// <summary>
        /// 撤销维护登记后将维护提醒的状态修改回 3：待处理
        /// </summary>
        /// <param name="auid"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public int AfterDelete(decimal auid, string assetNo)
        {
            return DalInstance.AfterDelete(auid, assetNo);
        }

        // 定义一个私有的对象作为锁
        private readonly object _lockObject = new object();

        /// <summary>
        /// 审核通过时，修改维护登记中的子表审核状态(StatusTag)
        /// </summary>
        /// <param name="warnModel"></param>
        /// <param name="userName"></param>
        public void UpdRequestWithAudit(AssetKeepWarn warnModel, string userName)
        {
            AssetUpkeepBll _auInstance = AssetUpkeepBll.Instance;
            lock (_lockObject)
            {
                // 1.通过 AU_ID,AssetNo 查询 维护登记子表数据
                AssetUpkeepBody aubModel = _auInstance.GetAssetUpkeepBody(warnModel.AU_ID, warnModel.AssetNo);
                if (aubModel != null)
                {
                    // 2.修改 aubModel中的StatusTag 为1
                    var b_body = _auInstance.UpdateStatusTag(aubModel.ID, 1, userName);
                }

                // 判断 HeadID 为 warnModel.AU_ID 的所有数据的 StatusTag 如果全部为1，则修改主表的审核状态为1（已完成）

                var aubList = _auInstance.GetBodyList(warnModel.AU_ID);
                if (aubList != null && aubList.Count > 0)
                {
                    // 使用Linq判断所有的StatusTag是否都为1（已审核）
                    var allAudited = aubList.All(a => a.StatusTag == 1);
                    if (allAudited)
                    {
                        // 则修改主表的审核状态为1
                        var b_head = _auInstance.UpdateStatus(warnModel.AU_ID, 1);
                    }
                }

            }
        }


        /// <summary>
        /// 定时任务批量新增
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public int AddList(List<AssetKeepWarn> list)
        {
            return DalInstance.AddList(list);
        }

    }
}
